var routDs;
var isSelectedRout = false;
var rForm;
Ext.onReady(function(){ 
	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect: true});	
	var recordType = Ext.data.Record.create([								
                			   {name: 'id', type: "string"},
							   {name: 'no', type: "string"},
                			   {name: 'fiberid', type: "string"},
							   {name: 'testDirection', type: "string"},
                	           {name: 'decibel', type: "string"},
                	           {name: 'allDb', type: "string"},
							   {name: 'beginPosition', type: "string"},
							   {name: 'beginMachineType', type: "string"},
                	           {name: 'direction', type: "string"},
							   {name: 'endPosition', type: "string"},
							   {name: 'endMachineType', type: "string"}
                	     ]);

	var cm  = new Ext.grid.ColumnModel(
			[
				sm,	             
				{header: "纤序", width: 50, sortable: true, dataIndex: 'no',align:'left'},	
	            {header: "测试方向", width: 100, sortable: true, dataIndex: 'testDirection',align:'left'},
	            {header: "Db/km", width: 100, sortable: true, dataIndex: 'decibel',align:'left'},
				{header: "总损耗（db）", width: 100, sortable: true, dataIndex: 'allDb',align:'left'},
				{header: "起点", width: 100, sortable: true, dataIndex: 'beginPosition',align:'left'},
	            {header: "设备规格", width: 100, sortable: true, dataIndex: 'beginMachineType',align:'left'}, 
				{header: "方向", width: 50, sortable: true,dataIndex: 'direction',align:'left'}, 	
				{header: "终点", width: 100, sortable: true, dataIndex: 'endPosition',align:'left'},
				{header: "设备规格", width: 100, sortable: true, dataIndex: 'endMachineType',align:'left'},
				{header: "删除", width: 50, dataIndex: 'id',renderer: showDelete}					
			]
		);
		
	function showDelete(value, cellmeta, record, rowIndex, columnIndex, store)
	{
		var ocid = record.get("ocid");
		return "<input type='button' value='删除' class='x-btn button' onclick='doRouteInfo(\"" + value  +   "\",\""+ocid+"\")'>";  
	}

	routDs = new Ext.data.Store({
        proxy: new Ext.data.DWRProxy(JFiberWeb.getFiberUsedInfoList, true),	
        reader: new Ext.data.ListRangeReader({
            totalProperty: 'totalSize',
            id: 'id'
        }, recordType),
        sortInfo:{field:'id', direction:'ASC'}// 排序信息
    });
	
	var rGrid = new Ext.grid.GridPanel({
	    	//el: 'contentsDiv',	
			region : 'center',			
	        ds: routDs,
	        cm: cm,
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) { 
							isSelectedRout = true;
                            rForm.getForm().loadRecord(rec);
                        }
                    }
                }),
			title: '光纤使用情况', 
			height: 400,
			minSize: 200,
	        frame:true,
	        collapsible:false, 
	        enableDragDrop:false, 
			bbar: new Ext.PagingToolbar({
			        pageSize: 17,
			        store: routDs,
			        displayInfo: true,
			        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			        emptyMsg: "没有记录"
			    })					
	    });
	//routDs.load({params:{id:"<%=strId%>",start:0,limit:17}});	
	
	rForm = new Ext.FormPanel({
        labelWidth: 95,
		region     : 'east', 
        frame:true,
        title: '总体信息', 
        width: 320,
		height   : 350,
		//autoHeight:true,
		autoScroll:true,
        defaults: {width: 180},
        defaultType: 'textfield',
        items: [
				{
					xtype : 'hidden',
					name: 'id'
				},
				{
					xtype : 'hidden',
					name: 'fiberid'
				},{
					xtype:'numberfield',
					fieldLabel: '纤序',
					name: 'no',
					value:1,
					allowBlank:false,
					blankText :'请输入纤序'	
				},{
					fieldLabel: '测试方向',
					name: 'testDirection',
					allowBlank:false,
					blankText :'请输入测试方向'	
				},{
					xtype:'numberfield',
					allowDecimals: true, 
					fieldLabel: 'Db/km',
					name: 'decibel',
					decimalPrecision:5,
					allowBlank:false,
					blankText :'请输入Db/km'	
				},
				{
					xtype:'numberfield',
					allowDecimals: true, 
					fieldLabel: '总损耗（db）',
					decimalPrecision:5,
					name: 'allDb'
				},{  
					fieldLabel: '起点',
					name: 'beginPosition'
				},{  
					fieldLabel: '设备规格',
					name: 'beginMachineType',
					allowBlank:false,
					blankText :'请输入设备规格'	
				},{  
					fieldLabel: '方向',
					name: 'direction',
					allowBlank:false,
					blankText :'请输入方向'	
				},{ 
					fieldLabel: '终点',
					name: 'endPosition',
					allowBlank:false,
					blankText :'请输入endPosition'	
				},
				{
					fieldLabel: '设备规格',
					name: 'endMachineType'
				}
        ],
        buttons: [
				{
					text: '后退',
					handler:function()
					{
						history.go(-1);
					}
				},{
					text: '保存',
					handler:saveSectionInfo
				},{
					text: '新建/取消',
					handler:function()
					{
						isSelectedRout = false ;
						rForm.getForm().reset(); 
						resetForm(rForm);	
					}
				}]
    });
	
	function saveSectionInfo()
	{
		if (!rForm.getForm().isValid())
			return;	
		var form = rForm.getForm().getValues(false);
		FiberUsedInfoBO = {};
		FiberUsedInfoBO.id = form.id;
		FiberUsedInfoBO.no = form.no;
		FiberUsedInfoBO.fiberid = ocid; //暂时存放中继段编号
		FiberUsedInfoBO.testDirection = form.testDirection;
		FiberUsedInfoBO.decibel = form.decibel;
		FiberUsedInfoBO.allDb = form.allDb;
		FiberUsedInfoBO.beginPosition = form.beginPosition;
		FiberUsedInfoBO.beginMachineType = form.beginMachineType;
		FiberUsedInfoBO.direction = form.direction;
		FiberUsedInfoBO.endPosition = form.endPosition;
		FiberUsedInfoBO.endMachineType = form.endMachineType;
		JFiberWeb.createFiberUsedInfo(FiberUsedInfoBO,function(data)
		{
			rForm.getForm().reset(); 
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				//routDs.load({params:{id:"<%=strId%>",start:0,limit:17}});
				rForm.getForm().reset();
				loadDate(routDs,rForm);	
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}	
		})
	}
	
	new Ext.Panel({
		width    : mainTabPanelDIV.getComputedWidth(),
		height   : mainTabPanelDIV.getComputedHeight()-20,
		layout   : 'border',
		renderTo : 'routingTab',
		items    : [
			rGrid,
			rForm
		]
	});
	loadDate(routDs,rForm);	
	
})

function doRouteInfo(strId,ocid)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该测试信息?', 
		function(btn) {
		 if(btn == 'yes') // 选中了是按钮	
		 {	
			 isSelectedRout = false;
			 rForm.getForm().reset(); 
			 JFiberWeb.DeleteFiberUsedInfo(strId, function(data) {
			 if(data) {							    	     		
				 //routDs.load({params:{id:"<%=strId%>",start:0,limit:17}});
				 loadDate(routDs,rForm);	
			 }else 
			 {
				Ext.Msg.alert("删除失败","数据删除失败!");
			 }
			 });
			
		 }
		}
	);
}